package com.sanchunhui.psc.ui.fragment

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.sanchunhui.psc.R
import com.sanchunhui.psc.bluetooth.ble.BleClientActivity
import com.sanchunhui.psc.bluetooth.bt.BtClientActivity
import com.sanchunhui.psc.common.BaseApplication
import com.sanchunhui.psc.common.BaseConstant
import com.sanchunhui.psc.ext.loadUrl
import com.sanchunhui.psc.ext.onClick
import com.sanchunhui.psc.ui.activity.*
import com.sanchunhui.psc.utils.GlideUtils
import kotlinx.android.synthetic.main.activity_user_info.*
import kotlinx.android.synthetic.main.fragment_me.*
import kotlinx.android.synthetic.main.fragment_me.mUserIconIv
import org.jetbrains.anko.startActivity


/*
    "我的"界面
 */
class MeFragment : BaseFragment(), View.OnClickListener {

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        super.onCreateView(inflater, container, savedInstanceState)
        return inflater.inflate(R.layout.fragment_me, null)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        initView()
    }

    /*
        初始化视图
     */
    private fun initView() {
        mUserIconIv.onClick(this)
        mUserNameTv.onClick(this)

        mDeviceTv.onClick(this)
        mCountTv.onClick(this)
        mRankingTv.onClick(this)
        mSettingTv.onClick(this)
    }

    override fun onStart() {
        super.onStart()
        loadData()
    }

    /*
        加载初始数据
     */
    private fun loadData() {
        val user = BaseApplication.mUser
        mUserNameTv.text = user.name
        mAccountTv.text = user.phone
        context?.let { GlideUtils.loadUrlImage(it, BaseConstant.IMAGE_SERVER_ADDRESS_URL+"${user.id%1000}/${user.id}.jpg",mUserIconIv) }
//        if (user.portrait != null) {
//            mUserIconIv.loadUrl(user.portrait.toString())
//        } else {
//            mUserIconIv.setImageResource(R.drawable.icon_default_user)
//        }
    }

    /*
        点击事件
     */
    override fun onClick(view: View) {
        when (view.id) {
            R.id.mUserIconIv, R.id.mUserNameTv -> {
                context!!.startActivity<UserInfoActivity>()
            }
            R.id.mDeviceTv -> {
                //BleClientActivity
                //BtClientActivity
                //BtServerActivity
                //DeviceListActivity
                context!!.startActivity<DeviceListActivity>()
            }
            R.id.mCountTv -> {
//                toast(R.string.coming_soon_tip)
                context!!.startActivity<MyTemActivity>()
            }
            R.id.mRankingTv -> {
//                toast(R.string.coming_soon_tip)
                context!!.startActivity<MyTemActivity>()
            }
            R.id.mSettingTv -> {
                context!!.startActivity<SettingActivity>()
            }
        }

    }

}
